package com.away.fuckingworld.task;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import com.away.fuckingworld.entity.DoubleColorBallHistory;
import com.away.fuckingworld.entity.dto.SSQCWLDTO;
import com.away.fuckingworld.service.DoubleColorBallService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 双色球信息获取任务
 * 通过中国福彩网站获取数据并入库
 */
@Slf4j
@Component
public class DoubleColorBallInfoGatherTask {

    private final static String CWL_URL = " http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice";

    private final static Long START_NUM = 2013001L;

    @Autowired
    private DoubleColorBallService doubleColorBallService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public synchronized void gatherInfo() {

//        http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=2000&issueEnd=2200&dayStart=&dayEnd=&pageNo=

         Long startNum = START_NUM;//从数据库中获取当前天数
        String lastCode = doubleColorBallService.getLastestCode();
        if(StringUtils.isNotBlank(lastCode)){
            startNum = Long.parseLong(lastCode);
        }
        Long endNum = startNum + 99;
//        System.setProperty("http.proxyHost", "127.0.0.1");
//        System.setProperty("http.proxyPort", "8888");
        String ssqInfo = gatherInfoFromCWL(startNum, endNum);


        JSONObject jsonObject = JSONObject.parseObject(ssqInfo);

        Long countNum = jsonObject.getLong("countNum");
        if (countNum == 0L) {
            return;
        }

        String result = jsonObject.getString("result");

        List<SSQCWLDTO> ssqcwldtos = JSONObject.parseArray(result, SSQCWLDTO.class);
        List<DoubleColorBallHistory> doubleColorBallHistoryList = ssqcwldtos.stream().map(SSQCWLDTO::toDoubleColorBallHistory).sorted(Comparator.comparing(DoubleColorBallHistory::getCode)).collect(Collectors.toList());
        doubleColorBallService.saveDoubleColorBallHistorys(doubleColorBallHistoryList);

    }

    public String gatherInfoFromCWL(Long startNum, Long endNum) {
        String info = HttpRequest.get(CWL_URL + "?name=ssq" + "&issueStart=" + startNum + "&issueEnd=" + endNum)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Host", "www.cwl.gov.cn")
                .header("X-Requested-With", "XMLHttpRequest")
                .header("Referer", "http://www.cwl.gov.cn/kjxx/ssq/kjgg/")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36")
                .cookie("Sites=_21; UniqueID=F0lEJUd5nE9qYRlg1606392347452; _ga=GA1.3.773556487.1606392347; _gid=GA1.3.1981548175.1607329644; _gat_gtag_UA_113065506_1=1; 21_vq=27")
                .execute().body();
        log.debug(info);
        if (StringUtils.isBlank(info)) {
            log.error("获取双色球信息失败");
            return null;
        }
        return info;
    }
}
